/*
 * AddCategoryFrame.java
 *
 * Created on __DATE__, __TIME__
 */

package com.insigma.yanwenlong.bookkeeping.gui;

import java.sql.SQLException;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

import com.insigma.yanwenlong.bookkeeping.pojo.Category;
import com.insigma.yanwenlong.bookkeeping.pojo.Direction;
import com.insigma.yanwenlong.bookkeeping.service.CategoryService;
import com.insigma.yanwenlong.bookkeeping.service.CategoryServiceImpl;
import com.insigma.yanwenlong.bookkeeping.service.DirectionService;
import com.insigma.yanwenlong.bookkeeping.service.DirectionServiceImpl;

/**
 *
 * @author  __USER__
 */
public class AddCategoryFrame extends javax.swing.JFrame {
	private DirectionService directionService = new DirectionServiceImpl();
	private CategoryService categoryService = new CategoryServiceImpl();
	/** Creates new form AddCategoryFrame */
	public AddCategoryFrame() {
		this.setTitle("添加类别");
		initComponents();
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		this.setLocationRelativeTo(null);
		//初始化类别反向
		initDirection();
	}

	private void initDirection() {
		try {
			List<Direction> directions = directionService.findDirections();
			DefaultComboBoxModel dcbm = (DefaultComboBoxModel) cobDirectin
					.getModel();
			for (Direction direction : directions) {
				dcbm.addElement(direction);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	//GEN-BEGIN:initComponents
	// <editor-fold defaultstate="collapsed" desc="Generated Code">
	private void initComponents() {

jLabel1 = new javax.swing.JLabel();
jtfCategory = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
cobDirectin = new javax.swing.JComboBox();
btnAdd = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("\u7c7b\u522b\u540d\u79f0\uff1a");

jLabel2.setText("\u7c7b\u522b\u65b9\u5411\uff1a");


btnAdd.setText("\u6dfb\u52a0");
btnAdd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnAddActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addGap(18, 18, 18)
.addComponent(jtfCategory, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel2)
.addGap(18, 18, 18)
.addComponent(cobDirectin, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addGroup(layout.createSequentialGroup()
.addGap(72, 72, 72)
.addComponent(btnAdd)))
.addContainerGap(40, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(36, 36, 36)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(jtfCategory, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(47, 47, 47)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(cobDirectin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
.addComponent(btnAdd)
.addGap(43, 43, 43))
);

pack();
}// </editor-fold>

	//GEN-END:initComponents

	//点击添加
	private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
		String categoryName = jtfCategory.getText();
		Direction direction = (Direction)cobDirectin.getSelectedItem();
		int directionId = direction.getId();
		
		Category category = new Category();
		category.setCategoryName(categoryName);
		category.setDirectionId(directionId);
		
		try {
			int rowAffected = categoryService.addCategory(category);
			if(rowAffected > 0) {
				JOptionPane.showMessageDialog(this, "添加类别成功");
			}else {
				JOptionPane.showMessageDialog(this, "添加类别失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				new AddCategoryFrame().setVisible(true);
			}
		});
	}

	//GEN-BEGIN:variables
	// Variables declaration - do not modify
	private javax.swing.JButton btnAdd;
	private javax.swing.JComboBox cobDirectin;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JTextField jtfCategory;
	// End of variables declaration//GEN-END:variables

}